﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Collections;

namespace PictureOrganizer2
{
    class Utility
    {
        private log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private const string comicGUIDFileName = "zindex.txt";
        internal static bool hasCellChanged(DataRow row, DataColumn col, bool ignoreWhitespace = true, StringComparison stringComparison = StringComparison.Ordinal)
        {
            bool isEqual = true;
            if (row[col, DataRowVersion.Original] != DBNull.Value && row[col, DataRowVersion.Current] != DBNull.Value)
            {
                string originalVersionToCompare = row[col, DataRowVersion.Original].ToString();
                string currentVersionToCompare = row[col, DataRowVersion.Current].ToString();
                if (ignoreWhitespace)
                {
                    originalVersionToCompare = originalVersionToCompare.Trim();
                    currentVersionToCompare = currentVersionToCompare.Trim();
                }
                isEqual = originalVersionToCompare.Equals(currentVersionToCompare, stringComparison);
            }
            else
                isEqual = row[col, DataRowVersion.Original].Equals(row[col, DataRowVersion.Current]);

            return !isEqual;
        }

        internal static string SHA1StringHash(string fileName)
        {
            System.Security.Cryptography.SHA1CryptoServiceProvider SHA1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
            byte[] Hash;
            string Result = string.Empty;
            string Tmp = string.Empty;

            FileStream FN = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, 8192);
            SHA1.ComputeHash(FN);
            FN.Close();

            Hash = SHA1.Hash;
            for (int i = 0; i < Hash.Length; i++)
            {
                Tmp = Convert.ToString(Hash[i], 16);
                if (Tmp.Length == 1)
                    Tmp = "0" + Tmp;
                Result += Tmp;
            }
            return Result;
        }

        internal static FileInfo[] GetFiles(DirectoryInfo dir, string searchPatterns, char[] separator)
        {
            ArrayList files = new ArrayList();
            string[] patterns = searchPatterns.Split(separator);
            foreach (string pattern in patterns)
            {
                if (pattern.Length != 0)
                {
                    files.AddRange(dir.GetFiles(pattern));
                }
            }
            return (FileInfo[])files.ToArray(typeof(FileInfo));
        }

        internal static string GetComicGUID(string dirPath)
        {
            string guid = String.Empty;
            string fileName = Path.Combine(dirPath, comicGUIDFileName);
            if (File.Exists(fileName))
            {
                using (StreamReader r = new StreamReader(fileName))
                {
                    guid = r.ReadToEnd();
                }
            }

            return guid;
        }

        internal static void SetComicGUID(string dirPath, string comicGUID)
        {
            using (StreamWriter s = new StreamWriter(Path.Combine(dirPath, comicGUIDFileName)))
            {
                s.Write(comicGUID);
            }
        }
    }
}
